package com.chart.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chart.chart.domian.req.UListReq;
import com.chart.chart.domian.resp.UserResp;
import com.chart.chart.domian.vo.ChartColorsVO;
import com.chart.chart.domian.vo.UserCoinLogVo;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.core.domain.entity.UserCoinLog;
import lombok.Data;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 统计mapper
 */
@Mapper
public interface UserCoinLogsMapper extends BaseMapper<UserCoinLog> {

    @Select("SELECT COUNT(*) > 0 FROM user_coin_log WHERE user_id = #{userId} AND action = #{action} AND created_at BETWEEN #{start} AND #{end}")
    boolean existsByUserIdAndActionAndCreatedAtBetween(@Param("userId") Long userId,
                                                       @Param("action") String action,
                                                       @Param("start") LocalDateTime start,
                                                       @Param("end") LocalDateTime end);

    @Select("SELECT COUNT(*) > 0 FROM user_coin_log WHERE user_id = #{userId} AND action = #{action}")
    boolean existsByUserIdAndAction(@Param("userId") Long userId, @Param("action") String action);

    @Select("SELECT * FROM user_coin_log WHERE user_id = #{userId} AND action = #{action} AND created_at BETWEEN #{start} AND #{end}")
    List<UserCoinLog> findByUserIdAndActionAndCreatedAtBetween(@Param("userId") Long userId,
                                                               @Param("action") String action,
                                                               @Param("start") LocalDateTime start,
                                                               @Param("end") LocalDateTime end);
    @Select("SELECT SUM(coins) FROM user_coin_log WHERE user_id = #{userId}")
    Integer getTotalCoinsByUserId(Long userId);


    @Select("SELECT SUM(number) FROM user_coin_log WHERE behavior = #{behavior} AND user_id = #{userId}")
    Integer getTotalByBehaviorAndUserId(@Param("behavior") String behavior, @Param("userId") Long userId);

    @Insert("INSERT INTO user_coin_log (user_id, operation_total) VALUES (#{userId}, #{operationTotal})")
    void operationPv(@Param("userId") Long userId, @Param("operationTotal") Long operationTotal);


    @Select("SELECT  SUM(operation_total)  FROM  user_coin_log WHERE user_id=#{userId}")
    Long operationTotal(@Param("userId") Long userId);

    @Insert("INSERT INTO user_coin_log (action,user_id) values (#{action},#{userId})")
    void insertOperationUv(@Param("userId") Long userId, @Param("action") String action);

    @Select("SELECT * FROM user_coin_log WHERE user_id=#{userId} AND action='OPERATION'")
    List<UserCoinLog> operationUvTotal(Long userId);

    @Insert("INSERT INTO user_coin_log (user_id, operation_total) VALUES (#{userId}, #{operationTotal})")
    void logPageView(@Param("userId") Long userId, @Param("operationTotal") Long operationTotal);

    @Insert("INSERT INTO user_coin_log (action, user_id) VALUES (#{action}, #{userId})")
    void logUniqueVisitor(@Param("userId") Long userId, @Param("action") String action);

    //@Select("SELECT SUM(operation_total) FROM user_coin_log WHERE user_id=#{userId}")
    @Select("SELECT SUM(operation_total) FROM user_coin_log")
    Long getTotalPageViews();

    @Select("SELECT COUNT(DISTINCT user_id) FROM user_coin_log WHERE action='OPERATION'")
    Long countUniqueVisitors();

    @Select("SELECT log_id, user_id, action, coins, created_at, behavior, number, operation_total, supplement FROM user_coin_log WHERE action = #{action}")
    List<UserCoinLog> getUserCoinLogsByAction(@Param("action") String action);

    @Select("SELECT COUNT(DISTINCT user_id) FROM user_coin_log WHERE action='SHARE'")
    Integer shereUv(@Param("userId") Long userId);

    @Insert("INSERT INTO user_coin_log (user_id, self_produce) VALUES (#{userId}, #{selfProduce})")
    void saveSelfProduce(@Param("userId") Long userId, @Param("selfProduce") Long selfProduce);

    @Select("SELECT SUM(self_produce) FROM user_coin_log WHERE user_id=#{userId}")
    Integer querySelfProduce(Long userId);

    @Insert("INSERT INTO user_coin_log (user_id, ai_self_produce) VALUES (#{userId}, #{aiSelfProduce})")
    void saveAiSelfProduce(@Param("userId") Long userId, @Param("aiSelfProduce") Long aiSelfProduce);

    @Select("SELECT SUM(ai_self_produce) FROM user_coin_log WHERE user_id=#{userId}")
    Integer queryAiSelfProduce(@Param("userId") Long userId);


    @Insert("INSERT INTO user_coin_log (user_id, configs_user_id, save) VALUES (#{userId}, #{configsUserId}, #{save})")
    void expertSave(@Param("userId") Long userId, @Param("configsUserId") Long configsUserId, @Param("save") Integer save);

    @Insert("INSERT INTO user_coin_log (user_id, configs_user_id, export) VALUES (#{userId}, #{configsUserId}, #{export})")
    void expertSaveExport(@Param("userId") Long userId, @Param("configsUserId") Long configsUserId, @Param("export") Integer export);


    @Select("SELECT u.configs_user_id,  c.chart_name AS config_name, SUM(u.save) AS save, SUM(u.export) AS export " +
            "FROM user_coin_log u " +
            "JOIN chart_configs_user c ON u.configs_user_id = c.id " +
            "GROUP BY u.configs_user_id,  c.chart_name")
    List<UserCoinLog> queryExpertSavePv( );



    // 统计 save 字段的 UV
    @Select("SELECT COUNT(DISTINCT user_id) " +
            "FROM user_coin_log " +
            "WHERE save IS NOT NULL")
    Integer queryExpertSaveUv();

    @Select("SELECT COUNT(DISTINCT user_id) " +
            "FROM user_coin_log " +
            "WHERE export IS NOT NULL")
    Integer queryExpertSaveUvs();



    @Select("SELECT ucl.configs_user_id AS configsUserId, " +
            "ccu.chart_name AS chartName, " +
            "COUNT(DISTINCT CASE WHEN ucl.save IS NOT NULL THEN ucl.user_id END) AS saveUv, " +
            "COUNT(DISTINCT CASE WHEN ucl.export IS NOT NULL THEN ucl.user_id END) AS exportUv " +
            "FROM user_coin_log ucl " +
            "JOIN chart_configs_user ccu ON ucl.configs_user_id = ccu.id " +
            "WHERE ucl.configs_user_id IS NOT NULL " +
            "GROUP BY ucl.configs_user_id, ccu.chart_name")
    List<Map<String, Object>> queryExpertSaveExportUV();

    @Insert("INSERT INTO user_coin_log (user_id, ai_save) VALUES (#{userId}, #{aiSave})")
    void saveAiSave(@Param("userId") Long userId, @Param("aiSave")  Long aiSave);


    @Select("SELECT SUM(ai_save) FROM user_coin_log WHERE user_id=#{userId}")
    Integer queryAiSave(@Param("userId") Long userId);



    @Select("SELECT SUM(seat_count) FROM  user_coin_log where  user_id=#{userId}  ")
    Integer querySeatCount( Long userId);




    List<UserCoinLogVo> getChartData(UListReq req);

    List<UserResp> querydemo();


    List<UserCoinLog> queryAction(@Param("phonenumber")String phonenumber,@Param("action") String action, @Param("startTime")String startTime,@Param("endTime") String endTime);

    List<UserCoinLogVo> queryExpertSaveExports(UListReq req);


    List<ChartColorsVO> queryAiColorPV(UListReq req);
}
